﻿<cfsilent>
	<cfscript>
	
		jsonAdvice = getProperty("serviceFactory").getBean("jsonAdvice");

		response = structNew();
		structInsert(response, "success", false, true);

		taskId = event.getArg("TSK");
		taskType = event.getArg("TT");
		
		step = event.getArg("STP");

		transaction {
			
			/* 载入教学任务 */
			switch(taskType){
				case 'P':
					sql = "SELECT  a.task_capacity,
								   a.task_limite,
								   a.task_chooesd 
							FROM t_task a 
							WHERE a.tsk_id = :taskId ";
					break;
				case 'C':
					sql = "SELECT  a.task_capacity,
								   a.task_limite,
								   a.task_chooesd 
							FROM t_common_task a 
							WHERE a.tsk_id = :taskId ";
					break;
				case 'R':
					sql = "SELECT  a.task_capacity,
								   a.task_limite,
								   a.task_chooesd 
							FROM t_reopen_task a 
							WHERE a.tsk_id = :taskId ";
					break;
				case 'T':
					sql = "SELECT  a.task_capacity,
								   a.task_limite,
								   a.task_chooesd 
							FROM t_sport_task a 
							WHERE a.tsk_id = :taskId ";
					break;
			}
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
			rs_task = queryObj.execute(sql = sql).getResult();
			PlanSize = rs_task.task_limite;
			if ( step eq "maxsize" ) {
				
				/*直接扩容为最大容量*/
				if ( rs_task.task_capacity gt 0 ) {
					
					newSize = rs_task.task_capacity;
					PlanSize = newSize;
					structInsert(response, "success", true, true);
					
				}
				else {
					structInsert(response, "reason", "无法读取教室容量", true);
				}
				
			}
			else if ( isNumeric(step) ) {
				
				if ( step gt 0 ) {
					
					if ( rs_task.task_capacity gt 0 ) {

						/* 增加计划人数 但在教室容量范围内 */
						newSize = min( rs_task.task_capacity, rs_task.task_limite + step );
					}
					else {
						newSize = rs_task.task_limite + step;
					}
					PlanSize = newSize;
					
					structInsert(response, "success", true, true);
				}

				if ( step lt 0 ) {
					
					/* 减少计划人数 但大于已选人数 */
					newSize = max( rs_task.task_chooesd, rs_task.task_limite + step );
					PlanSize = newSize;
					
					structInsert(response, "success", true, true);
				}
				
				if ( step eq 0 ) {
					structInsert(response, "success", true, true);
				}
				
			}
			else {
				structInsert(response, "reason", "错误的参数", true);
			}
			switch(taskType){
				case 'P':
					sql = "UPDATE t_task a 
							SET a.task_limite = :PlanSize 
							WHERE a.tsk_id = :taskId ";
					break;
				case 'C':
					sql = "UPDATE t_common_task a 
							SET a.task_limite = :PlanSize 
							WHERE a.tsk_id = :taskId ";
					break;
				case 'R':
					sql = "UPDATE t_reopen_task a 
							SET a.task_limite = :PlanSize 
							WHERE a.tsk_id = :taskId ";
					break;
				case 'T':
					sql = "UPDATE t_sport_task a 
							SET a.task_limite = :PlanSize  
							WHERE a.tsk_id = :taskId ";
					break;
			}
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name ="PlanSize", value=PlanSize, cfsqltype="cf_sql_interger");
			queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
			queryObj.execute(sql = sql);
			
			switch(taskType){
				case 'P':
					sql = "SELECT  a.task_capacity,
								   a.task_limite,
								   a.task_chooesd 
							FROM t_task a 
							WHERE a.tsk_id = :taskId ";
					break;
				case 'C':
					sql = "SELECT  a.task_capacity,
								   a.task_limite,
								   a.task_chooesd 
							FROM t_common_task a 
							WHERE a.tsk_id = :taskId ";
					break;
				case 'R':
					sql = "SELECT  a.task_capacity,
								   a.task_limite,
								   a.task_chooesd 
							FROM t_reopen_task a 
							WHERE a.tsk_id = :taskId ";
					break;
				case 'T':
					sql = "SELECT  a.task_capacity,
								   a.task_limite,
								   a.task_chooesd 
							FROM t_sport_task a 
							WHERE a.tsk_id = :taskId ";
					break;
			}
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
			rs_taskNow = queryObj.execute(sql = sql).getResult();
			structInsert(response, "planSize", rs_taskNow.task_limite, true);
		}
	
	
	</cfscript>
</cfsilent><cfoutput>#jsonAdvice.encode(data:response, stringNumbers:true)#</cfoutput>